package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.AlbumStat;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.model.base.BaseEntity;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.*;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Petrichor
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoServiceImpl extends ServiceImpl<TrackInfoMapper, TrackInfo> implements TrackInfoService {

	private final TrackInfoMapper trackInfoMapper;

	private final TrackStatMapper trackStatMapper;

	private final AlbumInfoMapper albumInfoMapper;

	private final AlbumStatMapper albumStatMapper;

	private final VodService   vodService;

	private final UserInfoFeignClient userInfoFeignClient;

	private final AlbumInfoService albumInfoService;
	@Autowired
	public TrackInfoServiceImpl(TrackInfoMapper trackInfoMapper, TrackStatMapper trackStatMapper, AlbumInfoMapper albumInfoMapper, AlbumStatMapper albumStatMapper, VodService vodService, UserInfoFeignClient userInfoFeignClient, AlbumInfoService albumInfoService) {
		this.trackInfoMapper = trackInfoMapper;
		this.trackStatMapper = trackStatMapper;
		this.albumInfoMapper = albumInfoMapper;
		this.albumStatMapper = albumStatMapper;
		this.vodService = vodService;
		this.userInfoFeignClient = userInfoFeignClient;
		this.albumInfoService = albumInfoService;
	}

	/**
	 * @description: 新增声音
	 * @author Petrichor
	 * @date 2023/12/22 18:33
	 * @param trackInfoVo  声音信息
	 * @return NULL
	 **/
	@Override
	public void saveTrackInfo(TrackInfoVo trackInfoVo) {
		Long userId = AuthContextHolder.getUserId();
		// 1. track_info 声音信息表
		TrackInfo trackInfo = new TrackInfo();
		trackInfo.setUserId(userId);
		//  通过前端传来的 声音信息 拷贝 赋值 [专辑id,声音标题,媒体文件的唯一标识,声音简介,是否开放]
		BeanUtils.copyProperties(trackInfoVo , trackInfo);
		// 请求云点播平台获取声音的相关数据
		TrackMediaInfoVo trackMediaInfoVo = vodService.getTrackMediaInfo(trackInfoVo.getMediaFileId());
		trackInfo.setMediaDuration(new BigDecimal(trackMediaInfoVo.getDuration()));
		// 赋值
		// 赋值 媒体播放地址
		trackInfo.setMediaUrl(trackMediaInfoVo.getMediaUrl());
		// 赋值 音频文件大小
		trackInfo.setMediaSize(trackMediaInfoVo.getSize());
		// 赋值 声音媒体类型
		trackInfo.setMediaType(trackMediaInfoVo.getType());
		/**
		 *  SELECT
		 * 			track.order_num
		 * 	FROM
		 * 			track_info track
		 * 	WHERE
		 * 			track.album_id = ?
		 * 	ORDER BY
		 * 			track.id
		 * 	DESC
		 * 	LIMIT  1
		 */
		LambdaQueryWrapper<TrackInfo> trackInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
		trackInfoLambdaQueryWrapper
				.select(TrackInfo::getOrderNum)
				.eq(TrackInfo::getAlbumId, trackInfoVo.getAlbumId())
				.orderByDesc(BaseEntity::getId)
				.last("limit 1");
		TrackInfo dbTrackInfo = trackInfoMapper.selectOne(trackInfoLambdaQueryWrapper);
		if(dbTrackInfo == null) {
			// 排序字段的值
			trackInfo.setOrderNum(1);
		}else {
			trackInfo.setOrderNum(dbTrackInfo.getOrderNum() + 1);
		}
		trackInfoMapper.insert(trackInfo);
		// 初始化统计数据(track_stat)
		this.saveTrackStat(trackInfo.getId() , SystemConstant.TRACK_STAT_PLAY);
		this.saveTrackStat(trackInfo.getId() , SystemConstant.TRACK_STAT_COLLECT);
		this.saveTrackStat(trackInfo.getId() , SystemConstant.TRACK_STAT_PRAISE);
		this.saveTrackStat(trackInfo.getId() , SystemConstant.TRACK_STAT_COMMENT);

		// 3. album_info 专辑表(更新专辑所包含的声音总数)
		AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfoVo.getAlbumId());
		albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount() + 1);
		albumInfoMapper.updateById(albumInfo) ;
	}
	/**
	 * @description: 获取当前用户声音分页列表
	 * @author Petrichor
	 * @date 2023/12/22 19:48
	 * @param page 当前页面
	 * @param limit 每页记录数
	 * @param trackInfoQuery   声音信息  赋值有 当前用户id
	 * @return 当前用户声音分页列表
	 **/
	@Override
	public IPage<TrackListVo> findUserTrackPage(Long page, Long limit, TrackInfoQuery trackInfoQuery) {
		Page<TrackListVo> trackListVoPage = new Page<>(page, limit);
		IPage<TrackListVo> trackListVoIPage = trackInfoMapper.selectUserTrackPage(trackListVoPage , trackInfoQuery);
		return trackListVoIPage;
	}
	/**
	 * @description: 修改声音
	 * @author Petrichor
	 * @date 2023/12/22 21:09
	 * @param  id 专辑id
	 * @param  trackInfoVo 声音信息
	 **/
	@Override
	public void updateTrackInfo(Long id, TrackInfoVo trackInfoVo) {
		// 修改
		TrackInfo dbTrackInfo = this.getById(id);
		// 前端传递过来的新的媒体文件的id
		String mediaFileId = trackInfoVo.getMediaFileId();
		// 获取之前的数据库中媒体文件的id
		String dbMediaFileId = dbTrackInfo.getMediaFileId();
		BeanUtils.copyProperties(trackInfoVo , dbTrackInfo);
		// 用户新上传了一个声音
		if(!dbMediaFileId.equals(mediaFileId)) {

			// 获取新的媒体文件的详情数据
			TrackMediaInfoVo trackMediaInfo = vodService.getTrackMediaInfo(mediaFileId);
			if(trackMediaInfo == null) {
				// 该异常会被全局异常处理器进行处理
				throw new GuiguException(ResultCodeEnum.VOD_FILE_ID_ERROR) ;
			}

			dbTrackInfo.setMediaDuration(new BigDecimal(trackMediaInfo.getDuration()));
			dbTrackInfo.setMediaFileId(mediaFileId);
			dbTrackInfo.setMediaUrl(trackMediaInfo.getMediaUrl());
			dbTrackInfo.setMediaSize(trackMediaInfo.getSize());
			dbTrackInfo.setMediaType(trackMediaInfo.getType());

			// 从云点播平台删除之前的声音
			vodService.removeMediaByFileId(dbMediaFileId) ;

		}

		// 进行修改
		this.updateById(dbTrackInfo) ;
	}
	/**
	 * @description: 删除声音信息
	 * @author Petrichor
	 * @date 2023/12/22 21:26
	 * @param id 专辑id
	 **/
	@Override
	public void removeTrackInfo(Long id) {
		//1. track_info 声音信息表
		// 获取声音对象数据
		TrackInfo trackInfo = this.getById(id);
		this.removeById(id);
		//更新专辑声音总数
		AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfo.getAlbumId());
		int includeTrackCount = albumInfo.getIncludeTrackCount() - 1;
		albumInfo.setIncludeTrackCount(includeTrackCount);
		albumInfoMapper.updateById(albumInfo);

		//2. 删除统计数据
		trackStatMapper.delete(new LambdaQueryWrapper<TrackStat>().eq(TrackStat::getTrackId, id));

		//3. 删除声音媒体
		vodService.removeMediaByFileId(trackInfo.getMediaFileId());
	}
	/**
	 * @description: 根据专辑id获取声音列表
	 * @author Petrichor
	 * @date 2024/1/1 14:25
	 * @param albumId 专辑id
	 * @param pageNo 页数
	 * @param limit 当前页的条数
	 * @return AlbumTrackListVo 用户专辑声音列表信息
	 **/
	@Override
	public IPage<AlbumTrackListVo> findAlbumTrackPage(Long albumId, Long pageNo, Long limit) {
		// 根据专辑的id查询专辑数据
		AlbumInfo albumInfo = albumInfoMapper.selectById(albumId);

		// 分页查询专辑所对应的声音数据
		Page<AlbumTrackListVo> page = new Page<>(pageNo , limit) ;
		IPage<AlbumTrackListVo> pageInfo = trackInfoMapper.findAlbumTrackPage(page , albumId);
		// 获取当前登录的用户id
		Long userId = AuthContextHolder.getUserId() ;
		if(userId == null) {
			// 用户没有登录
			if(!SystemConstant.ALBUM_PAY_TYPE_FREE.equals(albumInfo.getPayType())) {
				// 不是免费专辑
				// 把免费的过滤走,剩下的标注
				pageInfo.getRecords().stream().
						filter(albumTrackListVo -> albumTrackListVo.getOrderNum() > albumInfo.getTracksForFree())
						.forEach(albumTrackListVo -> albumTrackListVo.setIsShowPaidMark(true));
			}
		}else {
			//用户登录了
			if(SystemConstant.ALBUM_PAY_TYPE_VIPFREE.equals(albumInfo.getPayType())) {
				// 专辑的付费类型为vip免费

				// 查询用户数据
				Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVo();
				UserInfoVo userInfoVo = userInfoVoResult.getData();
				if(userInfoVo.getIsVip() == 0 ) {
					// 不是vip

					page.getRecords().stream().
							filter(albumTrackListVo -> albumTrackListVo.getOrderNum() > albumInfo.getTracksForFree())
							.forEach(albumTrackListVo -> albumTrackListVo.setIsShowPaidMark(true));
				}else {
					// 是vip

					// 判断vip是否过期
					Date vipExpireTime = userInfoVo.getVipExpireTime();
					long currentTimeMillis = System.currentTimeMillis();
					if( vipExpireTime != null) {
						if(vipExpireTime.getTime() < currentTimeMillis) {
							// vip过期了
							page.getRecords().stream().filter(albumTrackListVo -> albumTrackListVo.getOrderNum() > albumInfo.getTracksForFree())
									.forEach(albumTrackListVo -> albumTrackListVo.setIsShowPaidMark(true));
						}
					}else {
						page.getRecords().stream().filter(albumTrackListVo -> albumTrackListVo.getOrderNum() > albumInfo.getTracksForFree())
								.forEach(albumTrackListVo -> albumTrackListVo.setIsShowPaidMark(true));
					}
					// 判断用户是否购买过专辑或者声音
					List<Long> trackIdList = page.getRecords().stream().
							filter(albumTrackListVo -> albumTrackListVo.getOrderNum() > albumInfo.getTracksForFree())
							.map(albumTrackListVo -> albumTrackListVo.getTrackId()).collect(Collectors.toList());

					/**
					 * 存储用户购买专辑或者声音的数据表：
					 * tingshu_user:
					 * 		1、user_paid_album： 记录的是用户购买专辑数据
					 * 		2、user_paid_track： 记录的是用户购买的声音数据
					 *
					 * 	Map<Long , Integer>:
					 * 		键存储的是声音的id
					 * 		值存储的是0或者1 ， 如果是1表示购买过当前的声音、如果是0表示没有购买过
					 */
					Result<Map<Long , Integer>> paidTrackResult = userInfoFeignClient.userIsPaidTrack( albumId,trackIdList) ;

					Map<Long, Integer> paidTrackMap = paidTrackResult.getData();
					page.getRecords().stream().filter(albumTrackListVo -> albumTrackListVo.getOrderNum() > albumInfo.getTracksForFree())
							.forEach(albumTrackListVo -> {
								Long trackId = albumTrackListVo.getTrackId();
								Integer isBuy = paidTrackMap.get(trackId);
								boolean isShowPaidMark = isBuy != 1;
								albumTrackListVo.setIsShowPaidMark(isShowPaidMark);
							});
				}

			} else if(SystemConstant.ALBUM_PAY_TYPE_REQUIRE.equals(albumInfo.getPayType())){
				// 付费的专辑

				// 判断用户是否购买过专辑或者声音
				List<Long> trackIdList = page.getRecords().stream().
						filter(albumTrackListVo -> albumTrackListVo.getOrderNum() > albumInfo.getTracksForFree())
						.map(albumTrackListVo -> albumTrackListVo.getTrackId()).collect(Collectors.toList());

				/**
				 * 存储用户购买专辑或者声音的数据表：
				 * tingshu_user:
				 * 		1、user_paid_album： 记录的是用户购买专辑数据
				 * 		2、user_paid_track： 记录的是用户购买的声音数据
				 *
				 * 	Map<Long , Integer>:
				 * 		键存储的是声音的id
				 * 		值存储的是0或者1 ， 如果是1表示购买过当前的声音、如果是0表示没有购买过
				 */
				Result<Map<Long , Integer>> paidTrackResult = userInfoFeignClient.userIsPaidTrack( albumId,trackIdList) ;

				Map<Long, Integer> paidTrackMap = paidTrackResult.getData();
				page.getRecords().stream().filter(albumTrackListVo -> albumTrackListVo.getOrderNum().intValue() > albumInfo.getTracksForFree())
						.forEach(albumTrackListVo -> {
							Long trackId = albumTrackListVo.getTrackId();
							Integer isBuy = paidTrackMap.get(trackId);
							boolean isShowPaidMark = isBuy == 1 ? false : true ;
							albumTrackListVo.setIsShowPaidMark(isShowPaidMark);
						});

			}
		}
		return page ;
	}
	/**
	 * @description: 更新声音播放量
	 * @author Petrichor
	 * @date 2024/1/1 16:30
	 * @param trackStatMqVo 内部有需要更新的
	**/
	@Override
	public void updateStatNum(TrackStatMqVo trackStatMqVo) {

		String statType = trackStatMqVo.getStatType();
		if(SystemConstant.TRACK_STAT_PLAY.equals(statType)) {
			// 播放量的统计维度

			// 更新专辑的播放量
			Long albumId = trackStatMqVo.getAlbumId();
			LambdaQueryWrapper<AlbumStat> albumStatLambdaQueryWrapper = new LambdaQueryWrapper<>() ;
			albumStatLambdaQueryWrapper.eq(AlbumStat::getAlbumId , albumId) ;
			albumStatLambdaQueryWrapper.eq(AlbumStat::getStatType , SystemConstant.ALBUM_STAT_PLAY) ;
			AlbumStat albumStat = albumStatMapper.selectOne(albumStatLambdaQueryWrapper);
			albumStat.setStatNum(albumStat.getStatNum() + trackStatMqVo.getCount());
			albumStatMapper.updateById(albumStat) ;

			// 更新声音的播放量
			LambdaQueryWrapper<TrackStat> trackStatLambdaQueryWrapper = new LambdaQueryWrapper<>() ;
			trackStatLambdaQueryWrapper.eq(TrackStat::getTrackId , trackStatMqVo.getTrackId()) ;
			trackStatLambdaQueryWrapper.eq(TrackStat::getStatType , SystemConstant.TRACK_STAT_PLAY) ;
			TrackStat trackStat = trackStatMapper.selectOne(trackStatLambdaQueryWrapper);
			trackStat.setStatNum(trackStat.getStatNum() + trackStatMqVo.getCount());
			trackStatMapper.updateById(trackStat) ;

		}

	}
	/**
	 * @description: 统计声音信息接口
	 * @author Petrichor
	 * @date 2024/1/1 16:42
	 * @param trackId 声音id
	 * @return TrackStatVo 用户声音统计信息
	 **/
	@Override
	public TrackStatVo getTrackStatVoByTrackId(Long trackId) {
		return trackInfoMapper.selectTrackStat(trackId);
	}
	/**
	 * @description: 获取用户声音分集购买支付列表
	 * @author Petrichor
	 * @date 2024/1/7 21:46
	 * @param trackId 声音id
	 * @return TrackPayVo 声音分集购买信息列表
	 **/
	@Override
	public List<TrackPayVo> findUserTrackPaidList(Long trackId) {
		// 获取声音对象
		TrackInfo trackInfo = trackInfoMapper.selectById(trackId);
		// 专辑id
		Long albumId = trackInfo.getAlbumId();
		// 获取到专辑对象
		AlbumInfo albumInfo = albumInfoService.getById(albumId);
		// 根据专辑id 远程调用 查看当前用户的 当前专辑购买的 集数 集合
		Result<List<Long>> trackIdListResult = userInfoFeignClient.findUserPaidTrackList(albumInfo.getId());
		List<Long> trackIdList = trackIdListResult.getData();
		// 获取到当前专辑{albumId}中大于当前声音的orderNum所对应的前声音Id的集合列表
		LambdaQueryWrapper<TrackInfo> trackInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
		// 专辑id相同
		trackInfoLambdaQueryWrapper.eq(TrackInfo::getAlbumId , albumInfo.getId()) ;
		// 声音id 要大于 当前声音在当前专辑的声音
		trackInfoLambdaQueryWrapper.gt(TrackInfo::getOrderNum , trackInfo.getOrderNum()) ;
		// 只查询声音id字段
		trackInfoLambdaQueryWrapper.select(TrackInfo::getId) ;
		// 结果虽然说TrackInfo 对象 但只有id 有值
		List<TrackInfo> trackInfoList = trackInfoMapper.selectList(trackInfoLambdaQueryWrapper);
		// 利用流 获取到当前专辑{albumId}中大于当前声音的orderNum所对应的前声音Id的集合列表
		List<Long> trackIdAllList = trackInfoList.stream()
				.map(TrackInfo::getId)
				.collect(Collectors.toList());
		// 计算需要分集的声音id集合
		List<Long> trackIdNoReaptList = new ArrayList<>();
		if(CollectionUtils.isEmpty(trackIdList)) {
			trackIdNoReaptList = trackIdAllList ;
		}else {
			trackIdNoReaptList = trackIdAllList.stream()
					// list.contains() 用于判断集合中 是否 包含指定的元素。list会将括号内的元素和list中存在的元素进行逐个比对，
					// 若有相等的，返回结果为true, 若没有则返回结果为false。
					.filter(currentTrackId -> !trackIdList.contains(currentTrackId))
					.collect(Collectors.toList()) ;
		}
		// 进行分集操作，构建响应结果数据
		List<TrackPayVo> trackPayVoList = new ArrayList<>() ;

		// 本集
		TrackPayVo currentTrackPayVo = new TrackPayVo() ;
		currentTrackPayVo.setPrice(albumInfo.getPrice());		// 购买类型为单机购买，那么此时price存储的就是当个声音的价格
		currentTrackPayVo.setName("本集");
		currentTrackPayVo.setTrackCount(0);
		trackPayVoList.add(currentTrackPayVo);

		// 后10集
		int count = trackIdNoReaptList.size();
		if(trackIdNoReaptList.size() > 0 && trackIdNoReaptList.size() <= 10) {
			TrackPayVo trackPayVoAfterCount = new TrackPayVo() ;
			trackPayVoAfterCount.setPrice(albumInfo.getPrice().multiply(new BigDecimal(count)));
			trackPayVoAfterCount.setName("后" + count + "集");
			trackPayVoAfterCount.setTrackCount(count);
			trackPayVoList.add(trackPayVoAfterCount);
		}

		// 19
		if(count > 10) {
			TrackPayVo trackPayVoAfterTen = new TrackPayVo() ;
			trackPayVoAfterTen.setPrice(albumInfo.getPrice().multiply(new BigDecimal(10)));
			trackPayVoAfterTen.setName("后10集");
			trackPayVoAfterTen.setTrackCount(10);
			trackPayVoList.add(trackPayVoAfterTen);
		}

		// 后20集
		if(count > 10 && count <= 20) {
			TrackPayVo trackPayVoAfterCount = new TrackPayVo() ;
			trackPayVoAfterCount.setPrice(albumInfo.getPrice().multiply(new BigDecimal(count)));
			trackPayVoAfterCount.setName("后" + count + "集");
			trackPayVoAfterCount.setTrackCount(count);
			trackPayVoList.add(trackPayVoAfterCount);
		}

		if(count > 20) {
			TrackPayVo trackPayVoAfterTwenty = new TrackPayVo() ;
			trackPayVoAfterTwenty.setPrice(albumInfo.getPrice().multiply(new BigDecimal(20)));
			trackPayVoAfterTwenty.setName("后20集");
			trackPayVoAfterTwenty.setTrackCount(10);
			trackPayVoList.add(trackPayVoAfterTwenty);
		}

		//后30集
		if(count > 20 && count <= 30) {
			TrackPayVo trackPayVoAfterCount = new TrackPayVo() ;
			trackPayVoAfterCount.setPrice(albumInfo.getPrice().multiply(new BigDecimal(count)));
			trackPayVoAfterCount.setName("后" + count + "集");
			trackPayVoAfterCount.setTrackCount(count);
			trackPayVoList.add(trackPayVoAfterCount);
		}

		if(count > 30) {
			TrackPayVo trackPayVoAfterThirty = new TrackPayVo() ;
			trackPayVoAfterThirty.setPrice(albumInfo.getPrice().multiply(new BigDecimal(30)));
			trackPayVoAfterThirty.setName("后30集");
			trackPayVoAfterThirty.setTrackCount(30);
			trackPayVoList.add(trackPayVoAfterThirty);
		}

		//后50集
		if(trackIdNoReaptList.size() > 30 && trackIdNoReaptList.size() <= 50) {
			TrackPayVo trackPayVoAfterCount = new TrackPayVo() ;
			trackPayVoAfterCount.setPrice(albumInfo.getPrice().multiply(new BigDecimal(count)));
			trackPayVoAfterCount.setName("后" + count + "集");
			trackPayVoAfterCount.setTrackCount(count);
			trackPayVoList.add(trackPayVoAfterCount);
		}

		// 最多购买50集;
		if(count > 50) {
			TrackPayVo trackPayVoAfterFifty = new TrackPayVo() ;
			trackPayVoAfterFifty.setPrice(albumInfo.getPrice().multiply(new BigDecimal(50)));
			trackPayVoAfterFifty.setName("后50集");
			trackPayVoAfterFifty.setTrackCount(50);
			trackPayVoList.add(trackPayVoAfterFifty);
		}

		// 返回数据
		return trackPayVoList;
	}
	/**
	 * @description: 批量获取下单付费声音列表
	 * @author Petrichor
	 * @date 2024/1/7 22:28
	 * @param trackId 声音id
	 * @param trackCount 购买集数
	 * @return 购买的声音列表
	 **/
	@Override
	public List<TrackInfo> findPaidTrackInfoList(Long trackId, Integer trackCount) {
		// 构建一个List集合对象，封装响应结果数据
		List<TrackInfo> trackInfoList = new ArrayList<>() ;
		// 根据声音Id 获取到声音对象
		TrackInfo trackInfo = this.getById(trackId);
		// 如果购买数量大于0
		if(trackCount > 0) {
			// 获取专辑id
			Long albumId = trackInfo.getAlbumId();
			// 获取已支付的声音id列表
			Result<List<Long>> trackIdListResult = userInfoFeignClient.findUserPaidTrackList(albumId);
			List<Long> trackIdList = trackIdListResult.getData();
			// 查询购买的声音的列表
			LambdaQueryWrapper<TrackInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
			// 专辑id相同
			lambdaQueryWrapper.eq(TrackInfo::getAlbumId , trackInfo.getAlbumId()) ;
			// 购买的声音id 得大于等于 传来的声音id
			lambdaQueryWrapper.gt(TrackInfo::getOrderNum , trackInfo.getOrderNum()) ;
			// 根据声音在集合中顺序 排序
			lambdaQueryWrapper.orderByAsc(TrackInfo::getOrderNum) ;
			// 购买的声音id 不能是已经购买过的
			if(trackIdList != null && trackIdList.size() > 0) {
				lambdaQueryWrapper.notIn(TrackInfo::getId , trackIdList) ;
			}
			// 只购买多少集
			lambdaQueryWrapper.last("limit " + trackCount) ;

			// 查询
			trackInfoList = trackInfoMapper.selectList(lambdaQueryWrapper);
		}else {
			trackInfoList.add(trackInfo);
		}
		// 返回
		return trackInfoList;
	}

	/**
	 * @description: 初始化声音统计数据
	 * @author Petrichor
	 * @date 2023/12/22 19:36
	 * @param trackId 声音id
	 * @param statType 统计类型：0701-播放量 0702-收藏量 0703-点赞量 0704-评论数
	**/
	private void saveTrackStat(Long trackId, String statType) {
		TrackStat trackStat = new TrackStat();
		trackStat.setTrackId(trackId);
		trackStat.setStatType(statType);
		trackStat.setStatNum(0);
		trackStatMapper.insert(trackStat);
	}
}
